package com.jxl.coreplay.core
{
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.media.Video;
	import flash.net.NetStream;
	import flash.utils.Timer;
	
	public class UIVideo extends UIComponent
	{
		
		protected var _video:Video;
		protected var _maintainAspectRatio:Boolean 		= true;
		
		protected var videoDebugOutline:Shape;
		protected var _showDebugVideoOutline:Boolean 	= false;
		
		private var streamToAttach:NetStream;
		
		public function get video():Video { return _video; }
		
		public function get showDebugVideoOutline():Boolean { return _showDebugVideoOutline; }
		public function set showDebugVideoOutline(value:Boolean):void
		{
			_showDebugVideoOutline = value;
			invalidateSize();
		}
		
		public function get maintainAspectRatio():Boolean { return _maintainAspectRatio; }
		public function set maintainAspectRatio(value:Boolean):void
		{
			_maintainAspectRatio = value;
			invalidateSize();
		}
		
		public function UIVideo():void
		{
			super();
		}
		
		protected override function getDefaultWidthAndHeight():void
		{
			width				= 640;
			height				= 480;
		}
		
		protected override function createChildren():void
		{
			super.createChildren();
			
			_video 				= new Video();
			_video.smoothing 	= false;
			addChild(_video);
			if(streamToAttach)
			{
				attachNetStream(streamToAttach);
				streamToAttach = null;
			}
		}
		
		protected override function size():void
		{
			super.size();
			
			if(_video.videoWidth == 0 || _video.videoHeight == 0)
			{
				_video.width        = width;
				_video.height       = height;
				return;
			}
			
			if(_maintainAspectRatio == true)
			{
				var aspectRatio:Number;
				if(_video.videoWidth >= _video.videoHeight)
				{
					aspectRatio					= _video.videoWidth / _video.videoHeight;
					var desiredHeight:Number 	= Math.min(width / aspectRatio, height);
					trace(">>>>>>>>>>");
					trace("width: " + width + ", height: " + height + ", video.videoWidth: " + video.videoWidth + ", video.videoHeight: " + video.videoHeight);
					trace("aspectRatio: " + aspectRatio + ", desiredHeight: " + desiredHeight);
					if(desiredHeight <= height)
					{
						trace("one");
						_video.width 			= width;
						_video.height 			= desiredHeight;
					}
					else
					{
						trace("two");
						_video.width 			= aspectRatio * height;
						_video.height 			= height;
					}
					trace("end video.width: " + video.width + ", video.height: " + video.height);
				}
				else
				{
					aspectRatio 				= _video.videoHeight / _video.videoWidth;
					var desiredWidth:Number 	= Math.min(height / aspectRatio, width);
					
					if(desiredWidth <= width)
					{
						_video.width 			= desiredWidth;
						_video.height 			= height;
					}
					else
					{
						_video.width 			= width;
						_video.height 			= aspectRatio * width;
					}
				}
			}
			else
			{
				_video.width 					= width;
				_video.height 					= height;
			}
			
			drawDebugVideoOutline();
			
			
		}
		
		protected function drawDebugVideoOutline():void
		{
			if(_showDebugVideoOutline == true)
			{
				if(videoDebugOutline == null)
				{
					videoDebugOutline = new Shape();
					addChild(videoDebugOutline);
					setChildIndex(videoDebugOutline, numChildren - 1);
					
				}
				var g:Graphics = videoDebugOutline.graphics;
				g.clear();
				g.lineStyle(4, 0xFF0000);
				g.drawRect(_video.x, _video.y, _video.videoWidth, _video.videoHeight);
				g.lineStyle(2, 0x0000FF);
				g.drawRect(_video.x, _video.y, _video.width, _video.height);
				g.endFill();
			}
			else
			{
				if(videoDebugOutline != null)
				{
					videoDebugOutline.graphics.clear();
					if(contains(videoDebugOutline) == true) removeChild(videoDebugOutline);
					videoDebugOutline = null;
				}
			}
		}
		
		public function attachNetStream(netStream:NetStream):void
		{
			if(_video)
			{
				_video.attachNetStream(netStream);
			}
			else
			{
				streamToAttach = netStream;
			}
		}
	}
}